﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.IO;

public partial class Register : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Session["user"] != null)
            {
                this.Log_in1.Visible = false;
                this.Log_out1.Visible = true;
            }
            else
            {
                this.Log_in1.Visible = true;
                this.Log_out1.Visible = false;
            }
            LoadCbb();
        }
        else
        {

        }
    }

    private void LoadCbb()
    {
        ProRegister pr = new ProRegister();

        //load cbb Department
        DataSet ds = pr.LoadDepartment();
        this.cbbDepartment.DataSource = ds.Tables[0];
        cbbDepartment.DataTextField = "department";
        cbbDepartment.DataValueField = "id";
        this.cbbDepartment.DataBind();

        //load cbb Position
        DataSet ds1 = pr.LoadPosition();
        this.cbbPosition.DataSource = ds1.Tables[0];
        cbbPosition.DataTextField = "position_name";
        cbbPosition.DataValueField = "id";
        this.cbbPosition.DataBind();
    }
    protected void cmdSend_Click(object sender, EventArgs e)
    {
        ProRegister pr = new ProRegister();
        if (pr.CheckUserExist(this.txtUserNameReg.Text) == true)
        {
            this.lblErrorMess.Text = Constant.MESSAGE_ERROR_USER_EXIST;
            return;
        }
        if (this.txtUserNameReg.Text.Trim() == null || this.txtUserNameReg.Text.Trim() == "")
        {
            this.lblErrorMess.Text = Constant.MESSAGE_ERROR_MISSING_USERNAME;
            return;
        }
        if (txtPasswordReg.Text != txtConfPasswordReg.Text || txtPasswordReg.Text == "" || txtPasswordReg.Text.Length < 6
            || txtConfPasswordReg.Text == "" || txtConfPasswordReg.Text.Length < 6)
        {
            this.lblErrorMess.Text = Constant.MESSAGE_ERROR_WRONG_PASSWORD;
            return;
        }
        UserInfo ui = FillUser();

        ProRegister reg = new ProRegister();
        Boolean bResult = reg.InsertAccount(ui);
        if (bResult == true)
        {
            Response.Redirect("Result.aspx?reg=" + Constant.PARA_REG_OK);
        }
        else this.lblErrorMess.Text = "không được";
    }
    protected void cmdReset_Click(object sender, EventArgs e)
    {
        this.txtUserNameReg.Text = "";
        this.txtPasswordReg.Text = "";
        this.txtConfPasswordReg.Text = "";
        this.txtAddress.Text = "";
        this.txtEmail.Text = "";
        this.txtFullName.Text = "";
        this.txtPhone.Text = "";
        this.txtDOB.Text = "";
        this.cbbDepartment.SelectedIndex = 1;
        this.cbbDepartment.SelectedItem.Text = "";
        this.cbbPosition.SelectedIndex = 1;
        this.cbbPosition.SelectedItem.Text = "";
    }
    private UserInfo FillUser()
    {
        UserInfo ui = new UserInfo();
        ui.StrUserName = this.txtUserNameReg.Text;
        ui.StrPassWord = this.txtPasswordReg.Text;
        ui.StrAddress = this.txtAddress.Text;
        //check email
        ui.StrEmail = this.txtEmail.Text;
        ui.StrFullName = this.txtFullName.Text;
        //check length of phone
        ui.StrPhone = this.txtPhone.Text;
        //check DoB
        ui.DateBirthday = DateTime.Parse(this.txtDOB.Text);
        ui.DepDepartment.Id = Int32.Parse(this.cbbDepartment.SelectedItem.Value);
        ui.DepDepartment.StrDepartment = this.cbbDepartment.SelectedItem.Text;
        ui.PosPosition.Id = Int32.Parse(this.cbbPosition.SelectedItem.Value);
        ui.PosPosition.StrPosition = this.cbbPosition.SelectedItem.Text;
        ui.StaStatus.Id = Constant.INT_STATUS_DEFAULT_USER;
        ui.StaStatus.StrStatus = Constant.STR_STATUS_DEFAULT_USER_STRING;
        //Scan_Signatural
        if (this.fupSinature.HasFile)
        {
            try
            {
                ui.ImgSignature = Path.GetFileName(fupSinature.FileName);
                fupSinature.SaveAs(Server.MapPath("~/") + ui.ImgSignature);
                //StatusLabel.Text = "Upload status: File uploaded!";
            }
            catch (Exception ex)
            {
                ;
                //StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
            }
        }

        return ui;
    }
}

